﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
namespace GameJam
{
    public class CameraCtrl : MonoBehaviour {
        private Camera main_camera;
        [System.NonSerialized]
        public List<TreePlayer> player_list;

        public float cam_min_y = -5;
        public float cam_min_x = -5;
        public float cam_max_y = 10;
        public float cam_max_x = 5;
        public float cam_move_speed = 3;
        public float cam_speedup_dis = 3;

		static CameraCtrl _instance;
		public static CameraCtrl Instance { get { return _instance; } }

		bool _bInFocusMode = false;
		public bool IsInFocusMode 
		{
			get{ return _bInFocusMode;}
			set{ _bInFocusMode = value;}
		}

		void Awake()
		{
			_instance = this;
		}

        // Use this for initialization
        void Start() {
            main_camera = Camera.main;
            player_list = new List<TreePlayer>();
     
        }

        // Update is called once per frame
        void Update()
        {
			if (_bInFocusMode)
				return;

            if (isGameOverMove || isStartGameMove) return;
            if (isPlayerFollowMove)
            {
                UpdatePlayerFollowMove();
            }

        }

        private float startGame_camera_size = 5f;
        private float startGameAnim_over_camera_size = 4;
        private bool isStartGameMove = false;
        private bool isCanMove = true;
        public void StartGameMove(Vector3 start_pos)
        {
            Debug.LogWarning("StartGameMove");
            main_camera.transform.localPosition = start_pos;
            isCanMove = true;
            isStartGameMove = true;
            StartCoroutine(UpdateStartGameMove());
        }

        public void SetCameraSize(float size)
        {
            main_camera.orthographicSize = size;
        }

        public void SetCameraIsCanMove(bool isCanMove)
        {
            this.isCanMove = isCanMove;
        }

        private IEnumerator UpdateStartGameMove()
        {
            main_camera.orthographicSize = startGame_camera_size;
            yield return new WaitForSeconds(1);
            float timer = 2.5f;
            //while (this.isCanMove)
            while(timer > 0)
            {
                Vector3 pos = main_camera.transform.localPosition;
                pos.y += Time.deltaTime * 3;
                main_camera.transform.localPosition = pos;
                timer -= Time.deltaTime;
                yield return new WaitForEndOfFrame();
            }
            yield return new WaitForSeconds(0.5f);
            timer = 3;
            float cur_camera_size = main_camera.orthographicSize;
            while (timer > 0)
            {
                Vector3 pos = main_camera.transform.localPosition;
                pos.y -= Time.deltaTime * 2f;
                if (pos.y <= -3f) pos.y = -3f;
                main_camera.transform.localPosition = pos;
                float result_size = cur_camera_size;
                cur_camera_size = main_camera.orthographicSize;
                if (Mathf.Abs(startGameAnim_over_camera_size - cur_camera_size) > 0.01f)
                {
                    float offset_size = (startGameAnim_over_camera_size - cur_camera_size) * Time.deltaTime;
                    if (offset_size >= 0.1) offset_size = 0.1f;
                    result_size = cur_camera_size + offset_size;
                }
                else
                {
                    break;
                }
                main_camera.orthographicSize = result_size;
                timer -= Time.deltaTime;
                yield return new WaitForEndOfFrame();

            }
            isStartGameMove = false;

        }

        private bool isGameOverMove = false;
        private float gameOverCameraSize = 7.5f;
        public void GameOverMove(string static_str)
        {
            Debug.LogWarning("GameOverMove");
            isGameOverMove = true;
            isPlayerFollowMove = false;
            StartCoroutine(UpdateGameOverMove(static_str));
        }

        private IEnumerator UpdateGameOverMove(string static_str)
        {
            float timer = 3;
            float cur_camera_size = main_camera.orthographicSize;

            this.transform.DOMove(new Vector3(0, 3, -20),3);

            while (timer > 0)
            {
                float result_size = cur_camera_size;
                cur_camera_size = main_camera.orthographicSize;
                if (Mathf.Abs(gameOverCameraSize - cur_camera_size) > 0.01f)
                {
                    float offset_size = (gameOverCameraSize - cur_camera_size) * Time.deltaTime;
                    if (offset_size >= 0.1) offset_size = 0.1f;
                    result_size = cur_camera_size + offset_size;
                }
                else
                {
                    break;
                }
                main_camera.orthographicSize = result_size;
                timer -= Time.deltaTime;
                yield return new WaitForEndOfFrame();
            }
            isGameOverMove = false;

            UIMgr.Instance.gameOverUI.Show(static_str);
        }

        private bool isPlayerFollowMove = false;
        public void StartPlayerFollow()
        {
            isPlayerFollowMove = true;
        }
        public void UpdatePlayerFollowMove()
        {



            Vector2 left_down_pos = new Vector2(int.MaxValue, int.MaxValue);
            Vector2 right_up_pos = new Vector2(int.MinValue, int.MinValue);
            foreach (TreePlayer p in player_list)
            {
                Vector3 hero_pos = p.transform.position;
                if (hero_pos.x < left_down_pos.x) left_down_pos.x = hero_pos.x;
                if (hero_pos.x > right_up_pos.x) right_up_pos.x = hero_pos.x;
                if (hero_pos.y < left_down_pos.y) left_down_pos.y = hero_pos.y;
                if (hero_pos.y > right_up_pos.y) right_up_pos.y = hero_pos.y;
            }


            #region 摄像机位移
            Vector2 move_target = (left_down_pos + right_up_pos) / 2;
            move_target.y = move_target.y + 0.5f;     //追踪的位置要稍微偏上一点

            if (move_target.y < cam_min_y) move_target.y = cam_min_y;
            else if (move_target.y > cam_max_y) move_target.y = cam_max_y;

            if (move_target.x < cam_min_x) move_target.x = cam_min_x;
            else if (move_target.x > cam_max_x) move_target.x = cam_max_x;

            float dist = Vector2.Distance(move_target, this.transform.localPosition);   //vector2距离，忽略z
            if (dist > 0.1f)
            {
                Vector2 move_diff = move_target - (Vector2)this.transform.localPosition;

                move_diff = move_diff.normalized;
                if (dist < cam_speedup_dis)
                {
                    //float speed = cam_move_speed * Mathf.Sin(Mathf.PI / 2 * dist / cam_speedup_dis);
                    float speed = cam_move_speed * (dist / cam_speedup_dis);
                    this.transform.Translate(move_diff * Time.deltaTime * speed);
                }
                else
                {
                    this.transform.Translate(move_diff * Time.deltaTime * cam_move_speed);
                }
            }
            #endregion

            float max_dist_x = Mathf.Abs(left_down_pos.x - right_up_pos.x);
            float max_dist_y = Mathf.Abs(left_down_pos.y - right_up_pos.y);
            float cur_cam_size = Camera.main.orthographicSize;
            float normalized_x = max_dist_x / 10;
            float normalized_y = max_dist_y / 5;

            if (normalized_y > 1) normalized_y = 1 + (normalized_y - 1) * 0.5f; //对y做特殊处理

            float normalized_max = 1;
            if (normalized_x > 1 && normalized_x > normalized_max) normalized_max = normalized_x;
            if (normalized_y > 1 && normalized_y > normalized_max) normalized_max = normalized_y;
            cur_cam_size = 4f * normalized_max;
            if (cur_cam_size > 6f) cur_cam_size = 6f;
            //Camera.main.orthographicSize = cur_cam_size;
            if (Mathf.Abs(Camera.main.orthographicSize - cur_cam_size) > 0.1f) Camera.main.orthographicSize = Camera.main.orthographicSize + (cur_cam_size - Camera.main.orthographicSize) * Time.deltaTime;
            else Camera.main.orthographicSize = cur_cam_size;
        }



    }
}